package com.coolrui.domain.dubbo.provider;

import cn.hutool.json.JSONUtil;
import com.coolrui.api.dto.UserListDTO;
import com.coolrui.common.system.ApiList;
import com.coolrui.domain.prometheus.UserServiceMetrics;
import com.coolrui.domain.user.entity.UserAccount;
import com.coolrui.domain.user.service.UserDomainService;
import com.coolrui.domain.dubbo.utils.UserConvertUtil;
import com.plumelog.core.TraceId;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.coolrui.api.dto.UserDTO;
import com.coolrui.api.service.UserService;

import java.util.List;

@DubboService
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDomainService userDomainService;
    @Autowired
    private UserServiceMetrics userServiceMetrics;
    @Autowired
    private UserConvertUtil userConvertUtil;

    @Override
    public UserDTO getUser(Long userId) {
        // 记录监控-请求指标
        userServiceMetrics.recordRequest("getUser");

        UserAccount user = userDomainService.getUser(userId);
        UserDTO userDTO = userConvertUtil.toUserDTO(user);
        log.info("<<== 获取用户信息getUser,TraceId={},userId={},userDTO={}", TraceId.logTraceID.get(), userId, JSONUtil.toJsonStr(userDTO));
        return userDTO;
    }

    @Override
    public UserListDTO getUserList(Integer page, Integer pageSize) {
        // 记录监控-请求指标
        userServiceMetrics.recordRequest("getUserList");

        log.info("获取用户列表getUserList, TraceId={}, page={}, pageSize={}", TraceId.logTraceID.get(), page, pageSize);
        ApiList<UserAccount> userList = userDomainService.getUserList(0L,page, pageSize);
        List<UserDTO> userListDTO = userConvertUtil.toUserDTO(userList.getList());
        log.info("<<== 获取用户列表, TraceId={}, userListDTO={}", TraceId.logTraceID.get(), JSONUtil.toJsonStr(userListDTO));
        return new UserListDTO(userList.getHasMore(), userListDTO);
    }
}
